<?php
require_once(ROOTDIR."/include/live.class.php");
require_once(ROOTDIR."/include/database/class.ServerQueries.php");
require_once(ROOTDIR."/include/objects/class.Server.php");

?>
<script src="javascript/ajax.js" language="javascript" type="text/javascript"></script>
<script src="javascript/functions.js" language="javascript" type="text/javascript"></script>
<script type="text/javascript">
function formVerify() {
  var errorFound = false;
  var alertMessage = "";

  // Validate IP
  var regex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  var serverIp = document.getElementById("serverIp").value;
  if(!serverIp.match(regex)) {
    alertMessage += "Server IP is invalid.\n\r";
    errorFound = true;
  }
  
  // Validate Port
  var regex = /^\d{1,8}$/;
  var serverPort = document.getElementById("serverPort").value;
  if(!serverPort.match(regex)) {
    alertMessage += "Server Port is invalid.\n\r";
    errorFound = true;
  }
  
  // We have an error, do not submit the form
  if(errorFound) {
    alert(alertMessage);
    return false;
  }
  
  return true;
}

function deleteVerify(serverId, serverName) {
  if(confirm("Do you really want to delete "+serverName+"?")) {
    document.getElementById("deleteServer"+serverId).submit();
  }
}
</script>
<?php

$serverQueries = new ServerQueries();

$error = false;

// If this is set, then that means a server is being added
if(isset($_POST['submitAdd'])) {

  // TODO: Verify that the server can be connected to

  $newServerId = $serverQueries->addServer($_POST['serverName'], $_POST['serverIp'], $_POST['serverPort'], $_POST['serverRcon'], $_POST['serverType']);
    
  if($newServerId < 0) {
    $error = true;
  }
}

// If a server is being deleted
if(isset($_POST['deleteServer'])) {
  $serverQueries->deleteServer($_POST['serverId']);
}
// Get list of server objects
$servers = $serverQueries->getServers();
?>
<div class="tborder">
  <div id="tableHead">
    <div><b>Server List</b></div>
  </div>
  
  <table id="serverListTable" class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
  <tr>
    <?php 
          foreach($servers as $server) {
          $liveData = new LIVE($server->getIp(),$server->getPort());
          $liveData->connect();
          $liveData->query_info();
          $serverData = $liveData->get_result();
          ?>
          <td class="colColor1">
          <?=$server->getName()?><br/>
          <?=$server->getIp()?>:<?=$server->getPort()?><br/>
          <?=$serverData['map']?>
          <?=$serverData['totalplayers']?>/<?=$serverData['maxplayers']?><br/>
          <?php
            $appNumber = 0;
            if($server->getType() == "cstrike") {
              $appNumber = 240;
            } else if($server->getType() == "dod") {
              $appNumber = 300;
            }
          ?>
          <a href='steam: "-applaunch <?=$appNumber?> -game <?=$server->getType()?> +connect <?=$server->getIp()?>:<?=$server->getPort()?>"'>Join Server</a>
          </td>
    <?php 
            if(($i+1)%4==0) {
              ?></tr><?php
            }
          }
          if(($i+1)%3==0) {
            ?><td class="colColor1"></td><?php
          }
          if(($i+1)%3==2) {
            ?><td class="colColor1"></td><td class="colColor1"></td><?php
          }
    ?>
  </tr>
  </table>
</div>
<?php
// Only those with full privs can remove or add servers to the list
if($fullPower) {
?>
<br/>
<br/>
<div class="tborder">
  <div id="tableHead">
    <div><b>Server Management List</b></div>
  </div>
  <table id="serverManagementTable" class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
    <tr>
      <th class="colColor1" width="1%" nowrap>Server ID</th>
      <th class="colColor2" width="1%" nowrap>Server Name</th>
      <th class="colColor1" width="1%" nowrap>IP</th>
      <th class="colColor2" width="1%" nowrap>Port</th>
      <th class="colColor1" width="1%" nowrap>Type</th>
      <th class="colColor2" width="1%" nowrap>RCON Password</th>
      <th class="colColor1" width="1%" nowrap>Save</th>
      <th class="colColor2" width="1%" nowrap>Delete</th>
    </tr>
    <?php foreach($servers as $server) {    
          ?>
          <tr>
            <td class="colColor1" width="1%" nowrap><?=$server->getId()?></td>
            <td class="colColor2" width="1%" nowrap><input type="text" id="serverName:<?=$server->getId()?>" name="serverName:<?=$server->getId()?>" value="<?=$server->getName()?>" size="60" maxlength="128"/></td>
            <td class="colColor1" width="1%" nowrap><input type="text" id="serverIp:<?=$server->getId()?>" name="serverIp:<?=$server->getId()?>" value="<?=$server->getIp()?>" size="20" maxlength="16"/></td>
            <td class="colColor2" width="1%" nowrap><input type="text" id="serverPort:<?=$server->getId()?>" name="serverPort:<?=$server->getId()?>" value="<?=$server->getPort()?>" size="10"/></td>
            <td class="colColor1" width="1%" nowrap>
              <select id="serverType:<?=$server->getId()?>" name="serverType:<?=$server->getId()?>">
                <?php if(strtolower($server->getType()) == "cstrike") { ?>
                  <option value="cstrike" selected>CS:S</option>
                <?php }else { ?>
                  <option value="cstrike">CS:S</option>
                <?php }
                      if(strtolower($server->getType()) == "tf2") { ?>
                  <option value="cstrike" selected>TF2</option>
                <?php }else { ?>
                  <option value="tf2">TF2</option>
                <?php }
                      if(strtolower($server->getType()) == "dod") { ?>
                  <option value="dod" selected>DOD:S</option>
                <?php }else { ?>
                  <option value="dod">DOD:S</option>
                <?php } ?>
              </select>
            </td>
            <td class="colColor2" width="1%" nowrap><input type="text" id="serverRcon:<?=$server->getId()?>"name="serverRcon:<?=$server->getId()?>" value="<?=$server->getRcon()?>" maxlength="40"/></td>
            <td id="save:<?=$server->getId()?>" class="colColor1" onclick="saveServer('<?=$server->getId()?>');" style="cursor:pointer;">
            <img src="images/tick.png"/>
            </td>
            <td class="colColor2" style="cursor:pointer;" onclick="deleteVerify('<?=$server->getId()?>', '<?=$server->getName()?>');">
            <form action="index.php?page=serverList" id="deleteServer<?=$server->getId()?>" name="deleteServer<?=$server->getId()?>" method="POST">
              <input type="hidden" name="serverId" id="serverId" value="<?=$server->getId()?>"/>
              <input type="hidden" name="deleteServer" value="1">
              <img src="images/cross.png"/>
            </form>
            </td>
          </tr>
    <?php } ?>
    
    <tr>
      <td class="colColor1" width="1%" nowrap>&nbsp;</td>
      <td class="colColor2" width="1%" nowrap></td>
      <td class="colColor1" width="1%" nowrap></td>
      <td class="colColor2" width="1%" nowrap></td>
      <td class="colColor1" width="1%" nowrap></td>
      <td class="colColor2" width="1%" nowrap></td>
      <td class="colColor1" ></td>
      <td class="colColor2"></td>
      </tr>
    
    <!-- This row is for adding a new server -->
    <tr>
      <form action="index.php?page=serverList" method="POST" onsubmit="return formVerify();">
      <td class="colColor1" width="1%" nowrap>Add New Server</td>
      <td class="colColor2" width="1%" nowrap><input type="text" name="serverName" id="serverName" value="" size="60" maxlength="128"/></td>
      <td class="colColor1" width="1%" nowrap><input type="text" name="serverIp" id="serverIp" value="" size="20" maxlength="16"/></td>
      <td class="colColor2" width="1%" nowrap><input type="text" name="serverPort" id="serverPort" value="27015" size="10"/></td>
      <td class="colColor1" width="1%" nowrap>
        <select id="serverType" name="serverType">
            <option value="cstrike">CS:S</option>
            <option value="dod">DOD:S</option>
        </select>
      </td>
      <td class="colColor2" width="1%" nowrap><input type="text" name="serverRcon" id="serverRcon" value="" maxlength="40"/></td>
      <td class="colColor1" ><input type="submit" name="submitAdd" id="submitAdd" value="Add"/></td>
      <td class="colColor2"></td>
      </form>
    </tr>
    <?php
      if($error) {
      ?><tr><td class="colColor1" colspan="9"><span class="error">Server Already Exists</span></td></tr><?php
      }
    ?>
  </table>
</div>
<?php
}
?>
